<?php 
	
	class UserController extends BaseController{
		function showDetail($id){
			//lay du lieu user thong qua id
			$user = User::find($id);
			$queries = DB::getQueryLog();
			$place = Place::find(4);
			//tao view
			return View::make('user.userinfo',array('user'=>$user, 'place' => $place));
		}

		function editProfile() {

			$user = User::find(Auth::user()->id);
			$abc = 0;

			$rules = array(
				'firstName' => 'required', 
				'lastName' 	=> 'required', 
				'username'	=> 'required',
				'email'  	=> 'required|email', 
				'birthday' 	=> 'required',
				'gender' 	=> 'required',
				'nickname'	=> 'required',
				'mobile'	=> 'required' 
			);
			$validator = Validator::make(Input::all(), $rules);
			if ($validator->fails()) {
				return Redirect::to('profile')
					->withErrors($validator) 
					->withInput(); 
			} else {
				$time = strtotime(Input::get('birthday'));

				$birthday = date('Y-m-d',$time);

				$user->mobile	= Input::get('mobile');
		        $user->nickname = Input::get('nickname');
		        $user->username = Input::get('username');
		        $user->email 	= Input::get('email');
		        $user->save();

		        $userInformation = $user->userInformation()->first();
		        $userInformation->firstname = Input::get('firstName');
		        $userInformation->lastname  = Input::get('lastName');
		        $userInformation->gender 	= Input::get('gender');
		        $userInformation->birthday	= $birthday;
		        $userInformation->about		= Input::get('about');

		        $user->userInformation()->save($userInformation);

		    	return View::make('user.profile',array('user' => $user));
			}
		}
		function status() {

			$rules = array(
				'conten' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails()) {
				return Redirect::to('status')
					->withErrors($validator) 
					->withInput(); 
			} else {
				$id_content_type = ContentType::where('content_type_name','=','post')->first()->id;

				$Content = new Content();
				$Content->content_type_id = $id_content_type;
				$Content->save();
				$IdContent = $Content->id;

				$IdActivityType = ActivityType::where('activity_type_name','=','create_status')->first()->id;

				$IDVsContent =  new IDVsContent();
				$IDVsContent->from_id = Auth::user()->id;
				$IDVsContent->content_id = $IdContent;
				$IDVsContent->activity_type_id = $IdActivityType;
				$IDVsContent->save();

				$Post = new Post();
				$Post->post_by = Auth::user()->id;
				$Post->post_to = Auth::user()->id;
				$Post->content = Input::get('conten');
				$Post->longitude = 1;
				$Post->latitude = 1;
				$Post->shareable = 1;
				$Post = Content::find($IdContent)->Post()->save();

				$Activity_Log = new ActivityLog();
				$Activity_Log->longitude = 1;
				$Activity_Log->latitude = 1;
				$Activity_Log->from_id = Auth::user()->id;
				$Activity_Log->to_id = Auth::user()->id;
				$Activity_Log->contents_id = $IdContent;
				$Activity_Log->activity_type_id = $IdActivityType;
				$Activity_Log->save();
				return View::make('user.status');
			}
		}
		function editStatus($idStatus) {

			$rules = array(
				'conten' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails()) {
				return Redirect::to('status')
					->withErrors($validator) 
					->withInput(); 
			} else {
				
				$IdActivityType = ActivityType::where('activity_type_name','=','edit_status')->first()->id;

				$IDVsContent =  new IDVsContent();
				$IDVsContent->from_id = Auth::user()->id;
				$IDVsContent->content_id = $IdContent;
				$IDVsContent->activity_type_id = $IdActivityType;
				$IDVsContent->save();

				$Post = Content::find($idStatus)->Post()->first();
				$Post->content = Input::get('conten');
				$Post->shareable = 1;
				Content::find($idStatus)->Post()->save($Post);

				$Activity_Log = new ActivityLog();
				$Activity_Log->longitude = 1;
				$Activity_Log->latitude = 1;
				$Activity_Log->from_id = Auth::user()->id;
				$Activity_Log->to_id = Auth::user()->id;
				$Activity_Log->contents_id = $idStatus;
				$Activity_Log->activity_type_id = $IdActivityType;
				$Activity_Log->save();
				return View::make('user.status');
			}
		}
	}